﻿
module WorkBasket.Design {
    declare var mvcModel: any;
    WorkbasketApp.controller('wbdsFormCTRL',
        function ($scope) {
            var SOC = WorkBasket.SOC;
            $scope.Workflows = mvcModel.Workflows;
            $scope.SelectedWorkflow = $scope.Workflows[0];
            $scope.Forms = null;
            $scope.SelectedForm = null;
            $scope.Properties = [];//[{ Name: "one", Value: "test1" }, { Name: "two", Value: "test2" }, { Name: "three", Value: "test3" }];
            $scope.SelectedControlID = null;
            $scope.Fields = null;

            $scope.NodeChange = function (obj) {
               // alert("node change");
            };

            $scope.OnWorkflowChange = function (workflow) {
                $scope.Forms = SOC.Forms().GetByParentKey(workflow.WorkflowID);
                $scope.SelectedForm = null;
                $scope.Fields = SOC.Fields().GetByParentKey(workflow.WorkflowID);

                
            };

            $scope.OnFormChange = function (form) {

                $scope.SelectedForm = form;
                WorkBasket.FormTree.SetScope(form,$scope);

                $("#jstree").jstree().refresh(false);
            };
            $scope.CreateNewForm = function () {
                var obj = SOC.Forms().SaveData(new WorkBasket.Entities.Form(0, "My New Form", $scope.SelectedWorkflow.WorkflowID, null));
                $scope.Forms.push(obj);

            }
            $scope.UpdateForm = function (form) {
                var obj = SOC.Forms().SaveData(form,form.FormID);
            }
            $scope.UpdateProperty = function (Property) {
 
                var SOC = WorkBasket.SOC;
                var obj: any = SOC.FormControls().GetByKey($scope.SelectedControlID);
                var o = {};
                //iterate through properties to build json string then parse it into object.
                for (var index in $scope.Properties) {
                  
                    o[$scope.Properties[index].Name] = $scope.Properties[index].Value;
        
  
                }
               
                obj.Properties =JSON.stringify(o);

                SOC.FormControls().SaveData(obj);


            }
              $scope.AddNode = function(ctlType) {
                  var SOC = WorkBasket.SOC;
                  var type = ctlType.replace(/\./g, '_');//make the full name so it will html encode
                  var ctl = SOC.Forms().GetByKey(type, null, "GetControlData");
                  WorkBasket.FormTree.addnode(ctl, $scope.SelectedForm.FormID);
 

              };

             

            
        })

}
 